package com.yc.cloud.admin.controller;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.admin.dto.request.HwDeviceCreateReqDTO;
import com.yc.cloud.admin.dto.request.HwDeviceUpdateReqDTO;
import com.yc.cloud.admin.dto.request.HwDeviceBindCameraReqDTO;
import com.yc.cloud.admin.vo.HwDeviceVo;
import com.yc.cloud.admin.service.HwDeviceService;
import com.yc.cloud.entity.HwCamera;
import com.yc.cloud.admin.vo.HwCameraVo;
import com.yc.cloud.admin.vo.HwDeviceTypeVo;
import com.yc.cloud.admin.dto.request.HwDevicePageReqDto;
import com.yc.cloud.api.enums.HwDeviceType;
import java.util.Arrays;
import java.util.stream.Collectors;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

/**
 * <p>
 * 硬件设备表 (AI盒子) 前端控制器
 * </p>
 *
 * @author kinggu
 * @since 2025-07-15
 */
@RestController
@Tag(name = "硬件设备管理")
@RequestMapping("/hw-device")
public class HwDeviceController {

    @Resource
    private HwDeviceService hwDeviceService;

   

    @Operation(summary = "创建硬件设备")
    @PostMapping
    public ResponseResult<Boolean> create(@Validated @RequestBody HwDeviceCreateReqDTO createReqDTO) {
        return ResponseResult.success(hwDeviceService.create(createReqDTO));
    }

    @Operation(summary = "更新硬件设备")
    @PutMapping("/{id}")
    public ResponseResult<Boolean> update(@PathVariable("id") Long id, @Validated @RequestBody HwDeviceUpdateReqDTO updateReqDTO) {
        updateReqDTO.setId(id);
        return ResponseResult.success(hwDeviceService.update(updateReqDTO));
    }

    @Operation(summary = "删除硬件设备")
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable("id") Long id) {
        return ResponseResult.success(hwDeviceService.removeById(id));
    }

    @Operation(summary = "获取硬件设备详情")
    @GetMapping("/{id}")
    public ResponseResult<HwDeviceVo> getById(@PathVariable("id") Long id) {
        return ResponseResult.success(hwDeviceService.getVOById(id));
    }

    @Operation(summary = "分页查询硬件设备")
    @GetMapping
    public ResponseResult<CommonPage<HwDeviceVo>> list(@Validated HwDevicePageReqDto pageReqDTO) {
        return ResponseResult.success(hwDeviceService.list(pageReqDTO));
    }

    @Operation(summary = "设备置顶")
    @PostMapping("/{id}/pin")
    public ResponseResult<Boolean> pin(@PathVariable("id") Long id) {
        return ResponseResult.success(hwDeviceService.pin(id));
    }

    @Operation(summary = "取消置顶")
    @PostMapping("/{id}/unpin")
    public ResponseResult<Boolean> unpin(@PathVariable("id") Long id) {
        return ResponseResult.success(hwDeviceService.unpin(id));
    }

    @Operation(summary = "绑定摄像头到设备")
    @PostMapping("/{id}/bind-cameras")
    public ResponseResult<Boolean> bindCameras(
            @PathVariable("id") @Parameter(description = "设备ID") Long id,
            @Validated @RequestBody HwDeviceBindCameraReqDTO bindReqDTO) {
        boolean success = hwDeviceService.bindCameras(id, bindReqDTO.getCameraIds());
        return ResponseResult.success(success);
    }

    @Operation(summary = "解绑设备下的所有摄像头")
    @PostMapping("/{id}/unbind-cameras")
    public ResponseResult<Boolean> unbindCameras(@PathVariable("id") @Parameter(description = "设备ID") Long id) {
        boolean success = hwDeviceService.unbindCameras(id);
        return ResponseResult.success(success);
    }

    @Operation(summary = "解绑指定摄像头")
    @PostMapping("/{id}/unbind-specific-cameras")
    public ResponseResult<Boolean> unbindSpecificCameras(
            @PathVariable("id") @Parameter(description = "设备ID") Long id,
            @Validated @RequestBody HwDeviceBindCameraReqDTO unbindReqDTO) {
        boolean success = hwDeviceService.unbindSpecificCameras(id, unbindReqDTO.getCameraIds());
        return ResponseResult.success(success);
    }

    @Operation(summary = "获取设备绑定的摄像头列表")
    @GetMapping("/{id}/bound-cameras")
    public ResponseResult<List<HwCameraVo>> getBoundCameras(@PathVariable("id") @Parameter(description = "设备ID") Long id) {
        List<HwCameraVo> cameras = hwDeviceService.getBoundCameras(id);
        return ResponseResult.success(cameras);
    }

    @Operation(summary = "检查摄像头是否已绑定到设备")
    @GetMapping("/{deviceId}/camera/{cameraId}/bound")
    public ResponseResult<Boolean> isCameraBound(
            @PathVariable("deviceId") @Parameter(description = "设备ID") Long deviceId,
            @PathVariable("cameraId") @Parameter(description = "摄像头ID") Long cameraId) {
        boolean bound = hwDeviceService.isCameraBound(deviceId, cameraId);
        return ResponseResult.success(bound);
    }

    @Operation(summary = "获取设备类型列表")
    @GetMapping("/types")
    public ResponseResult<List<HwDeviceTypeVo>> getDeviceTypes() {
        List<HwDeviceTypeVo> deviceTypes = Arrays.stream(HwDeviceType.values())
                .map(type -> new HwDeviceTypeVo(type.getCode(), type.getDescription()))
                .collect(Collectors.toList());
        return ResponseResult.success(deviceTypes);
    }

    @Operation(summary = "刷新设备列表数据")
    @PostMapping("/refresh")
    public ResponseResult<Boolean> refresh() {
        return ResponseResult.success(hwDeviceService.refreshData());
    }
}
 